import java.io.*;
import java.util.Scanner;

public class ParseFilesApp {
    private String pathDir,strKey;
    // private StringBuffer outString = new StringBuffer(2048);
    private File [] fileList;

    public void insertDirPath() throws IOException {
        System.out.println("Insert dir path>");
        BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
        this.pathDir = d.readLine();
    }


    public void checkDir(){
        File someFile = new File(this.pathDir);
        if(someFile.exists()){
            if(someFile.isDirectory()){
                System.out.println("Loading files ");
            }else{
                System.out.println("Incorrect path");
                System .exit(0);
            }
        }else {
            System.out.println("Incorrect path");
            System .exit(0);
        }
    }

    public void insertKey() throws IOException {
        System.out.println("Insert key>");
        BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
        this.strKey = d.readLine();
    }

    public void lookForFiles(){
        File dir = new File(this.pathDir);
        this.fileList = dir.listFiles();
    }

    public void parseFile(File file) throws IOException {
        /*  Scanner scan = new Scanner(file);
        String keyString = "==="+this.strKey+"===";
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            System.out.println(line);
            int indexStart = line.lastIndexOf(keyString,0);
            int indexEnd = line.indexOf("===",indexStart);
            this.outString.append(line.substring(indexStart, indexEnd));
        }*/
        String startToken = "==="+this.strKey+"===";
        String endToken = "===";
        boolean output = false;

        Scanner scan = new Scanner(file);
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            if (!output && line.indexOf(startToken) > -1) {
                output = true;
                line = line.substring(line.indexOf(startToken)+startToken.length());
            } else if (output && line.indexOf(endToken) > -1) {
                output = false;
                System.out.println(line.substring(0, line.indexOf(endToken)));
            }

            if (output) {
                System.out.println(line);
            }
        }


    }

    public void parseFiles() throws IOException {
        for(File i:this.fileList){
            parseFile(i);
        }

    }


    public static void main(String [] args){

        ParseFilesApp firstTry = new ParseFilesApp();
        try {
            firstTry.insertDirPath();
            firstTry.checkDir();
            firstTry.insertKey();
            firstTry.lookForFiles();
            firstTry.parseFiles();
            // System.out.println(firstTry.outString);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

